import React, { Component } from 'react'
import store from '../redux/store'
import {createDecrementAction,createIncrementAction,createAsyncAction} from '../redux/count_action'

export default class Count extends Component {
  incrementCount=()=>{
    const {value}=this.selectNumber
    // store.dispatch({type:'increment',data:value*1})
    store.dispatch(createIncrementAction(value*1)) //引入action.js文件之后就不需要在手动些action对象
  }
  decrementCount=()=>{
    const {value}=this.selectNumber
    // store.dispatch({type:'decrement',data:value*1})
    store.dispatch(createDecrementAction(value*1))
  }
  incrementCountIfOdd=()=>{
    const {value}=this.selectNumber
    if(store.getState()%2===0){
      // store.dispatch({type:'increment',data:value*1})
      store.dispatch(createIncrementAction(value*1))
    }
  }
  incrementCountAsync=()=>{
    const {value}=this.selectNumber
    // setTimeout(()=>{
    //   store.dispatch(createIncrementAction(value*1))
    // },1000)
    store.dispatch(createAsyncAction(value*1,1000))
  }
  render() {
    return (
      <div>
        <h1>当前求和为:{store.getState()}</h1>
        <select ref={c=>this.selectNumber=c}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>&nbsp;
        <button onClick={this.incrementCount}>+</button>&nbsp;
        <button onClick={this.decrementCount}>-</button>&nbsp;
        <button onClick={this.incrementCountIfOdd}>偶数时再加</button>
        <button onClick={this.incrementCountAsync}>等待完再加</button>
      </div>
    )
  }
}
